//
//  MYGMyPageHeaderView.swift
//  MYDoctor
//
//  Created by Mo on 2024/3/7.
//

import UIKit

class MYGMyPageHeaderView: UIView {
    var myHeaderImageV:UIImageView!
    var myNameLabel:UILabel!
    var myInfoLabel:UILabel!
    var numberLbs = [UILabel]()
    var headerActionBlock:intBlock?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
//        let titleLab = UILabel()
//        titleLab.text = "我的"
//        titleLab.font = UIFont.systemFont(ofSize: SCALEWIDTH(18), weight: .regular)
//        titleLab.textColor = .black
//        self.addSubview(titleLab)
//        titleLab.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(statusBarHeight + SCALEWIDTH(12.5))
//            make.height.equalTo(SCALEWIDTH(25))
//        }
                
        myHeaderImageV = UIImageView()
        myHeaderImageV.layer.masksToBounds = true
        myHeaderImageV.layer.cornerRadius = SCALEWIDTH(35)
        myHeaderImageV.contentMode = .scaleAspectFill
        myHeaderImageV.sd_setImage(with: URL(string: MYGUserManager.getUser()?.doctor.avatar), placeholderImage: UIImage(named: "MYG_myPage_touxiang"), context: nil) 
        self.addSubview(myHeaderImageV)
        myHeaderImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(18))
            make.top.equalToSuperview().offset(SCALEWIDTH(28.5))
            make.height.width.equalTo(SCALEWIDTH(70))
        }
        
        myNameLabel = UILabel()
        myNameLabel.textColor = RGBA(r: 34, g: 34, b: 34, a: 1)
        myNameLabel.font = UIFont.systemFont(ofSize: 22, weight: .medium)
        myNameLabel.text = MYGUserManager.getUser()?.doctor.name
        self.addSubview(myNameLabel)
        myNameLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(SCALEWIDTH(43.5))
            make.left.equalTo(myHeaderImageV.snp.right).offset(SCALEWIDTH(10))
            make.height.equalTo(SCALEWIDTH(22))
        }
        
        myInfoLabel = UILabel()
        myInfoLabel.textColor = RGBA(r: 96, g: 102, b: 114, a: 1)
        myInfoLabel.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        let hospital = MYGUserManager.getUser()?.doctor.hospital ?? ""
        let title = MYGUserManager.getUser()?.doctor.title ?? ""
        myInfoLabel.text = "\(hospital) \(title)"
        self.addSubview(myInfoLabel)
        myInfoLabel.snp.makeConstraints { make in
            make.top.equalTo(myNameLabel.snp.bottom).offset(SCALEWIDTH(8))
            make.left.equalTo(myHeaderImageV.snp.right).offset(SCALEWIDTH(10))
            make.height.equalTo(SCALEWIDTH(12))
        }
        let arr = ["好友","关注","粉丝"]
        for ind in 0...2{
            let numLab = UILabel()
            numLab.textColor = RGBA(r: 51, g: 51, b: 51, a: 1)
            numLab.font = UIFont.systemFont(ofSize: 18, weight: .medium)
            numLab.text = "100"
            numLab.textAlignment = .center
            numLab.isUserInteractionEnabled = true
            numLab.tag = 100 + ind
            numberLbs.append(numLab)
            let tap1 = UITapGestureRecognizer(target: self, action: #selector(tapAction(ges:)))
            numLab.addGestureRecognizer(tap1)
            self.addSubview(numLab)
            numLab.snp.makeConstraints { make in
                make.top.equalTo(myHeaderImageV.snp.bottom).offset(SCALEWIDTH(23.5))
                make.left.equalToSuperview().offset(Int(screenWidth)/3*ind)
                make.height.equalTo(SCALEWIDTH(25))
                make.width.equalTo(screenWidth/3)
            }
            
            let nameLabel = UILabel()
            nameLabel.textColor = RGBA(r: 173, g: 178, b: 200, a: 1)
            nameLabel.font = UIFont.systemFont(ofSize: 13, weight: .regular)
            nameLabel.text = arr[ind]
            nameLabel.textAlignment = .center
            nameLabel.isUserInteractionEnabled = true
            nameLabel.tag = 100 + ind
            let tap2 = UITapGestureRecognizer(target: self, action: #selector(tapAction(ges:)))
            nameLabel.addGestureRecognizer(tap2)
            self.addSubview(nameLabel)
            nameLabel.snp.makeConstraints { make in
                make.top.equalTo(numLab.snp.bottom).offset(SCALEWIDTH(5))
                make.left.equalToSuperview().offset(Int(screenWidth)/3*ind)
                make.height.equalTo(SCALEWIDTH(18.5))
                make.width.equalTo(screenWidth/3)
            }
            if ind < 2{
                let lineV = UIView()
                lineV.backgroundColor = RGBA(r: 173, g: 178, b: 200, a: 1)
                self.addSubview(lineV)
                lineV.snp.makeConstraints { make in
                    make.left.equalToSuperview().offset(Int(screenWidth)/3*(ind + 1))
                    make.top.equalTo(numLab.snp.top).offset(SCALEWIDTH(15.5))
                    make.width.equalTo(SCALEWIDTH(0.5))
                    make.height.equalTo(SCALEWIDTH(15.5))
                }
            }
        }
        
//        let taskBtn = UIButton()
//        taskBtn.setImage(UIImage(named: "MYG_myPage_task"), for: .normal)
//        self.addSubview(taskBtn)
//        taskBtn.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(SCALEWIDTH(6))
//            make.right.equalToSuperview().offset(SCALEWIDTH(-6))
//            make.bottom.equalToSuperview()
//            make.height.equalTo(SCALEWIDTH(80))
//        }
    }
    
    func bindData(_ data:MYGStatistics) {
        for(_,obj) in numberLbs.enumerated() {
            if obj.tag == 100 {
                obj.text = "\(data.myFriends)"
            }
            if obj.tag == 101 {
                obj.text = "\(data.followers)"
            }
            if obj.tag == 102 {
                obj.text = "\(data.following)"
            }
        }
    }
    @objc func tapAction(ges:UITapGestureRecognizer){
        let tag = ges.view?.tag
        if self.headerActionBlock != nil{
            self.headerActionBlock!(tag ?? 0)
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
